﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SNPAD
{
    public partial class Frm_Rework : Form
    {
        string trackStatus = "";//
        public Frm_Rework()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        private void Rework()
        {
            try
            {
                string sOleDb = "";
                ItemObject coSelected = (ItemObject)cmbWork.SelectedItem; 
                string reworkStatus = coSelected.Value ;
                if(Convert.ToInt32(reworkStatus )>= Convert.ToInt32(trackStatus ))
                {
                    MessageBox.Show("再投入工序错误！\r\n只能投入前面的工序");
                    return;
                }
                switch (reworkStatus)
                {
                    case "00":
                        sOleDb = "delete hv_bd where fan_code='" + txtFan_Code.Text + "'";
                        OleDbHelper3.ExecuteNonQuery(sOleDb);

                        sOleDb = "delete hv_sd where fan_code='" + txtFan_Code.Text + "'";
                        OleDbHelper3.ExecuteNonQuery(sOleDb);

                        sOleDb = "delete ph_balance where fan_code='" + txtFan_Code.Text + "'";
                        OleDbHelper3.ExecuteNonQuery(sOleDb);
                        break;
                    case "11":
                        sOleDb = "delete hv_bd where fan_code='" + txtFan_Code.Text + "'";
                        OleDbHelper3.ExecuteNonQuery(sOleDb);

                        sOleDb = "delete hv_sd where fan_code='" + txtFan_Code.Text + "'";
                        OleDbHelper3.ExecuteNonQuery(sOleDb); 
                        break;
                    case "22":
                        sOleDb = "delete hv_bd where fan_code='" + txtFan_Code.Text + "'";
                        OleDbHelper3.ExecuteNonQuery(sOleDb);
                        break;
                      
                    default:
                        return; 
                }
                 

                sOleDb = "update jc_fan set track_status ='" + reworkStatus + "' where fan_code='" + txtFan_Code.Text  + "'";
                OleDbHelper3.ExecuteNonQuery(sOleDb);


                sOleDb = "insert into HV_ZTR (fan_code,oldTrack_status,NewTrack_status) values('"+txtFan_Code.Text +"','" + trackStatus +"','" + reworkStatus + "')";
                OleDbHelper3.ExecuteNonQuery(sOleDb);


                trackStatus = "";
                txtFan_Code.Text = "";
                MessageBox.Show("再投入成功！");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void CheckBarcode(string barcode)
        {
            string retmsg = "";
            string sOleDb = @"select track_status from jc_fan Where fan_code='" + barcode + "'";
            try
            {
                object Status = OleDbHelper3.ExecuteScalar(sOleDb, null);
                if (Status != null && !Convert.IsDBNull(Status))//有重复单号
                {
                    trackStatus = Status.ToString().Trim();
                    switch (trackStatus)
                    {
                        case "00":
                            retmsg = "打标完成状态";
                            break;
                        case "11":
                            retmsg = "平衡测试OK状态";
                            break;
                        case "22":
                            retmsg = "松动测试OK状态";
                            break;
                        //case "50":
                        //    retmsg = "SV电特测试OK状态";
                        //    break;
                        case "55":
                            retmsg = "绑定验证OK状态";
                            break;
                        default:
                            retmsg = "条码状态未知（" + Status.ToString().Trim() + ")";
                            break;
                    }
                }
                else//没有这个条码
                {
                    retmsg = "没有这个条码";
                }
                lblWork.Text = retmsg;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Frm_Rework_Load(object sender, EventArgs e)
        {
            ItemObject[] io = new ItemObject[3];
            //ItemObject[] io = new ItemObject[3];
            io[0] = new ItemObject("00", "打标完成状态");
            io[1] = new ItemObject("11", "平衡测试OK状态");
            io[2] = new ItemObject("22", "松动测试OK状态");
            //io[3] = new ItemObject("50", "SV电特测试OK状态");
            //io[4] = new ItemObject("55", "绑定验证OK状态");

            cmbWork.Items.AddRange(io);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            CheckBarcode(txtFan_Code.Text.Trim());
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            Rework();
        }
    }
}
